package bridge;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner;

import javax.management.RuntimeErrorException;

public class Controller {

	private static Runtime runtime;
	private static Process process;
	private String term;
	private static String path = System.getProperty("user.home") + "/util";

	public Controller(String term) {
		this.term = term;
	}

	/**
	 * applica l'algoritmo di type inference al termine preso in input e poi
	 * richiama il risolutore di vincoli .
	 * 
	 * @param term
	 *            termine
	 * @throws IOException
	 * @throws InterruptedException
	 */
	public static void infDlal(String term) throws IOException,
			InterruptedException {
		runtime = Runtime.getRuntime();
		term = "\"" + term + ";;\"";

		process = runtime.exec(new String[] { "bash", "-c",
				"cd " + path + "&& ./script.sh const  term " + term });
	}

	
	public static void lciInterpreter(String term) throws IOException,
			InterruptedException {
		runtime = Runtime.getRuntime();
		term = " \"" + term + "\"";
		File res = new File(path + "/interpResult");
		if (res.exists())
			res.delete();

		process = runtime.exec(new String[] { "bash", "-c",
				"cd " + path + " && ./lciscript.sh  " + term });
		
		InputStream inp = process.getInputStream();
	    int n =0;
	    while((n=inp.read())!=-1)
	    		System.out.print((char)n);
	    //process.destroy();
		
	}

	
	
	/*
	public static void solveConstraint() throws IOException,
			InterruptedException {
		runtime = Runtime.getRuntime();
		Process process = runtime.exec(new String[] { "bash", "-c",
				"cd " + path + " && glpsol --cpxlp const  -o solution " });

	}
*/
}
